

function Drag() {
    this.oDiv = document.querySelector("#div1");
    this.disX = 0;
    this.disY = 0;
}

//
Drag.prototype.init = function () {
    var This = this;
    //改变fnDown里面的this指向
    this.oDiv.onmousedown = function (ev) {
        This.fnDown(ev);
    };
}

Drag.prototype.fnDown = function (ev) {
    var ev = ev || event;
    this.disX = ev.clientX - this.oDiv.offsetLeft;
    this.disY = ev.clientY - this.oDiv.offsetTop;

    var This = this;
    document.onmousemove = function (ev) {
        This.fnMove(ev);
    };
    document.onmouseup = this.fnUp;
    return false;
}

Drag.prototype.fnUp = function () {
    document.onmousemove = null;
    document.onmouseup = null;
};

Drag.prototype.fnMove = function (ev) {
    var ev = ev || event;
    this.oDiv.style.left = ev.clientX - this.disX + "px";
    this.oDiv.style.top = ev.clientY - this.disY + "px";
}


    var d1 = new Drag();
    d1.init();
